﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
    <style>
        div
        {
            padding-top:10px;
            padding-bottom:10px;
            }
    </style>
    <script type="text/javascript" src="/Scripts/jquery.js"></script>
    <script type="text/javascript">
        var wait = false, handle = false;
        function changedatabase(sender) {
            $.ajax({
                url: '/home/showdatabase',
                data: { database: $(sender).val() },
                dataType: 'json',
                beforeSend: function () {
                    wait = true;
                },
                success: function (result) {
                    wait = false;
                    $('#changedbname').html('');
                    for (var i = 0, len = result.length; i < len; i++) {
                        $('#changedbname').append(
                            $('<option name="' + result[i] + '">' + result[i] + '</option>')
                        );
                    }
                    $('#changedbname').trigger('change');
                    handle = false;
                }
            });
        }
        function changedatabasename(sender) {
            $.ajax({
                url: '/home/showtables',
                data: { database: $('#sltdatabase').val(), databaseName: $(sender).val() },
                dataType: 'json',
                beforeSend: function () {
                    wait = true;
                },
                success: function (result) {
                    wait = false;
                    var str = '';
                    for (var i = 0, len = result.length; i < len; i++) {
                        str += result[i] + '<br/>';
                    }
                    $('#txttables').html(str);
                }
            });
        }
    </script>
        @if(ViewBag.Post)
        {
        <script type="text/javascript">
            handle = true;
            $(function () {
                $('#sltdatabase').val('@ViewBag.database');
                $('#sltdatabase').trigger('change');
                var inv = window.setInterval(function () {
                    if (!handle) {
                        $('#changedbname').val('@ViewBag.databasename');
                        $('#changedbname').trigger('change');
                        window.clearInterval(inv);
                    }
                }, 200);
            });
        </script>
        }
</head>
<body>
<ul>
    <li><a href="/">DB Administrator</a></li>
    <li><a href="/home/file">File Adminisrator</a></li>
</ul>
    <form action="/home/index" method="post">
        <div>
            <select name="database" id="sltdatabase" onchange="changedatabase(this)">
                <option value="sqlserver" selected="selected">SQL Server</option>
                <option value="mysql">MySQL</option>
            </select> --
            <select name="databasename" id="changedbname" onchange="changedatabasename(this)">
            @{
                DbAdministrator.Modals.DbInfo info = ViewBag.Db;
                foreach (var item in info.dblist)
                {
                    <option value="@item">@item</option>
                }
                }
            </select>
        </div>
        <div>
            <div id="txttables" name="tables" style=" float:left; border:1px solid #ccc; overflow:scroll; margin-right:5px; width:220px;height:205px;">
            @{
                foreach (var item in info.firsttables)
                {
                    @item <br />
                }
                }
            </div>
            <textarea name="command" style="width:600px; height:220px;">@ViewBag.command</textarea>
        </div>
        <div>
            <input type="submit" value="Excute" />
        </div>
    </form>
    @if (ViewBag.Post)
    {
    <hr />
        if (ViewBag.Result == null)
        {
    <div>
        @Html.Raw(ViewBag.Error)
    </div>
        }
        else
        {
            if (ViewBag.ResultType == "table")
            {
                System.Data.DataTable dt = ViewBag.Result;
                if (dt != null)
                {
                <table border="1">
                    <tr>
                        @foreach (System.Data.DataColumn cln in dt.Columns)
                        {
                            <td>
                                @cln.ColumnName
                            </td>
                        }
                    </tr>
                    @foreach (System.Data.DataRow row in dt.Rows)
                    {
                       <tr>
                       @for (int i = 0, len = dt.Columns.Count; i < len; i++)
                       {
                        <td>
                            @(row[i] == null ? "&nbsp;" : row[i])
                        </td>
                       }
                       </tr>
                    }
                </table>
                }
            }
            else
            { 
                    <span>影响行数： @ViewBag.Result</span>
            }
        }
    }
    
</body>
</html>
